package com.dao.impl;

import com.bean.Customer;
import com.bean.Supplier;
import com.dao.SupplierDao;
import com.util.JDBC_Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SupplierDaoImpl implements SupplierDao {

    Connection conn = null;
    ResultSet rs= null;
    PreparedStatement ps = null;

    @Override
    public List<Supplier> getAllSupplier() {
        conn = JDBC_Util.getConnection();
        List<Supplier> customers = new ArrayList<>();
        String sql = "select id, supplier_name, director, director_tel, address, manage_range from t_supplier";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String customerName = rs.getString("supplier_name");
                String director = rs.getString("director");
                String directorTel = rs.getString("director_tel");
                String address = rs.getString("address");
                String manageRange = rs.getString("manage_range");

                Supplier customer = new Supplier();
                customer.setId(id);
                customer.setAddress(address);
                customer.setDirector(director);

                customer.setDirectorTel(directorTel);
                customer.setManageRange(manageRange);
                customer.setName(customerName);
                customers.add(customer);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return customers;
    }

    @Override
    public int addSupplier(String customerName,String director,String directorTel,
                           String address,String manageRange) {
        String sql = "insert into t_supplier values (null,'"+customerName+"','"+director+"'," +
                "'"+directorTel+"','"+address+"','"+manageRange+"')";
        int m = JDBC_Util.update(sql);
        return m;
    }

    @Override
    public int updateSupplier(int key, String tel, String director) {
        String sql = "update t_supplier set director_tel = '"+tel+"', director = '"+director+"'" +
                " where id = "+key+"";
        int m = JDBC_Util.update(sql);
        return m;
    }

    @Override
    public Supplier getSupplierInfo(int key) {
        conn = JDBC_Util.getConnection();
        Supplier customer = new Supplier();
        String sql = "select id, supplier_name, director, director_tel, address, manage_range from t_supplier" +
                " where id = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,key);
            rs = ps.executeQuery();
            if (rs.next()){
                int id = rs.getInt("id");
                String customerName = rs.getString("supplier_name");
                String director = rs.getString("director");
                String directorTel = rs.getString("director_tel");
                String address = rs.getString("address");
                String manageRange = rs.getString("manage_range");

                customer.setId(id);
                customer.setAddress(address);
                customer.setDirector(director);
                customer.setDirectorTel(directorTel);
                customer.setManageRange(manageRange);
                customer.setName(customerName);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return customer;
    }

    @Override
    public List<Supplier> findSupplier(String supplierName, String manageRange) {
        List<Supplier> list = new ArrayList<>();
        conn = JDBC_Util.getConnection();

        String sql ="select id, supplier_name, director, director_tel, address, manage_range" +
                " from t_supplier where 1 = 1";

        if(supplierName != "" && !supplierName.equals(null)){
            sql += " and supplier_name like '%"+supplierName+"%'";
        }

        if(manageRange != "" && !manageRange.equals(null)){
            sql += " and manage_range like '%"+manageRange+"%'";
        }

        try {
            ps = conn.prepareStatement(sql);

            rs = ps.executeQuery();

            while (rs.next()){
                int id = rs.getInt("id");
                String acustomerName = rs.getString("supplier_name");
                String director = rs.getString("director");
                String directorTel = rs.getString("director_tel");
                String address = rs.getString("address");
                String amanageRange = rs.getString("manage_range");

                Supplier customer = new Supplier();
                customer.setId(id);
                customer.setAddress(address);
                customer.setDirector(director);
                customer.setDirectorTel(directorTel);
                customer.setManageRange(amanageRange);
                customer.setName(acustomerName);
                list.add(customer);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBC_Util.close(rs,conn,ps);
        }
        return list;
    }
}
